<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>出库申请登记</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
	</head>

	<body>
		<!--
			弹出框相关代码
		-->
		<form class="layui-form layui-form-pane" lay-filter="dataFrm" id="dataFrm">

			<div style="width: 100%;">
				<p style="font-size: 28px; margin: 3% 0% 2% 40%;">出库申请登记</p>
			</div>
			<div style="margin: 10px 10px 10px 10px">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">申请人</label>
						<div class="layui-input-inline">
							<input type="text" name="storer" id="storer" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">出库理由</label>
						<div class="layui-input-block">
							<select name="reason" id="reason" class="providerSel">
								<option value="" selected="selected">请选择出库理由</option>
								<option value="C002-1">生产领料</option>
								<option value="C002-2">赠送</option>
								<option value="C002-3">内部借领</option>
								<option value="C002-4">其他借领</option>
							</select>
						</div>
					</div>
					<div class="layui-inline">
						<label class="layui-form-label">备注</label>
						<div class="layui-input-inline">
							<input type="text" id="remark" name="remark" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-inline">
						<a class="layui-btn customer_btn_add layui-btn-sm layui-icon layui-icon-add-1" lay-event="edit" id="tocke">添加产品</a>
					</div>
				</div>
			</div>
			<table id="ProductFile" lay-filter="test"></table>
			<!--修改的弹出层-->
			<div class="layui-row" id="update" style="display:none;">
				<div class="layui-col-md10" align="center">
					<form class="layui-form layui-from-pane" style="margin-top: 20px;" method="" id="accountFomat">
						<table id="demo" lay-filter="test"></table>
						<!--隐藏的用户id-->
						<!-- <input type="text" name="accountId" id="accountId" hidden>
						<div class="layui-form-item" style="margin-top:40px">
							<div class="layui-input-block">
								<input class="layui-btn" type="button" lay-submit="*" lay-filter="dataForm" value="提交">
								<button type="reset" class="layui-btn layui-btn-primary">返回<button>
							</div>
						</div> -->
					</form>
				</div>
			</div>
			<div>
				<table id="ProductFile" lay-filter="table" class="tableIns"></table>
			</div>
			<!--    -->
			<div class="layui-input-block" style=" margin:3% 0 0 45%;" id="tj">
				<a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label class="layui-icon layui-icon-release">提交</label></a>
			</div>
		</form>
		<div style="display: none;" id="formulate3">
			<input type="text" name="title" id="title" value="0" required lay-verify="required" placeholder="输入数量" , class="layui-input">
		</div>

		<!-- <input type="text" name="title" id="title" value="0" required placeholder="输入数量" class="layui-input"> -->

		<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
		<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
		<script src="../../../lib/common/common.js" charset="utf-8"></script>
		<script type="text/javascript">
			layui.use(['table', 'jquery', 'form'], function() {
				var ckcpxx = [];
				var oldount;
				var table = layui.table,
					layer = layui.layer,
					form = layui.form,
					$ = layui.jquery;
				//监听行工具事件
				$("#title").on("input", function(e) {
					console.log("11111")
					//获取input输入的值
					alert(e.delegateTarget.value);

				});
				table.on('tool(test)', function(obj) {

					var ok;
					var data = obj.data;
					oldount = data;
					console.log(oldount)
					if (ckcpxx.length != 0) {
						for (var i = 0; i < ckcpxx.length; i++) {
							if (ckcpxx[i].rcProductId == data.rcProductId) {
								layer.msg("请勿重复添加", {
									icon: 5
								})
								ok = 0;
								break;
							} else {
								ok = 1;
							}
						}
					} else {
						ckcpxx.push(data);
					}
					if (ok == 1) {
						ckcpxx.push(data)
					}
					table.init('table', {
						cols: [
							[{
								field: 'rcStoreName',
								title: '库房名称',
								width: 160,
								align: 'center'
							}, {
								field: 'rcProductId',
								title: '产品编号',
								width: 190,
								align: 'center'
							}, {
								field: 'rcProductName',
								title: '产品名称',
								width: 180,
								align: 'center'
							}, {
								field: 'rcConfing',
								title: '产品描述',
								width: 180,
								align: 'center'
							}, {
								field: 'rcAmount',
								title: '产品数量',
								width: 150,
								align: 'center',
								edit:true
							}, {
								field: 'prdCostPrice',
								title: '产品单价（元）',
								width: 140,
								align: 'center'
							}, {
								field: 'rcAmountUnit',
								title: '产品单位',
								width: 110,
								align: 'center'
							}, {
								title: '操作',
								width: 180,
								templet: '#formulate',
								align: 'center'
							}]
						],
						data: ckcpxx
					});
				});
				table.init('table', {
					cols: [
						[{
								field: 'rcStoreName',
								title: '库房名称',
								width: 160,
								align: 'center'
							}, {
								field: 'rcProductId',
								title: '产品编号',
								width: 190,
								align: 'center'
							}, {
								field: 'rcProductName',
								title: '产品名称',
								width: 180,
								align: 'center'
							}, {
								field: 'rcConfing',
								title: '产品描述',
								width: 180,
								align: 'center'
							}, {
								field: 'rcAmount',
								title: '产品数量',
								width: 150,
								align: 'center',
								edit:true
							}, {
								field: 'prdCostPrice',
								title: '产品单价（元）',
								width: 140,
								align: 'center'
							}, {
								field: 'rcAmountUnit',
								title: '产品单位',
								width: 110,
								align: 'center'
							}, {
							title: '操作',
							width: 180,
							templet: '#formulate',
							align: 'center'
						}]
					],
				});
				

				var pd=new Array();


				function lopen() {
					layer.open({
						type: 1,
						title: '出库产品',
						area: ['900px', '400px'],
						content: $('#update') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
					});
				}
				$("#tocke").click(function() {
					lopen();
				})
				table.on('tool(table)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
					var data = obj.data; //获得当前行数据
					// if(data.amount>100){
					// }
					var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
					var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
					layer.confirm('是否取消出库？', function(index) {
						obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
						layer.close(index);
						for (var i = 0; i < ckcpxx.length; i++) {
							if (ckcpxx[i].productId == data.productId) {
								ckcpxx.splice(i, 1)
								break;
							}
						}
					});
				});
				table.render({
					elem: '#demo',
					width: 900,
					url: api2 +'outrepertory/QueryRepertoryCell' //数据接口
						,
					page: true //开启分页
						,
					cols: [
						[ //表头
							{
								field: 'rcStoreName',
								title: '库房名称',
								width: 160,
								align: 'center'
							}, {
								field: 'rcProductId',
								title: '产品编号',
								width: 190,
								align: 'center'
							}, {
								field: 'rcProductName',
								title: '产品名称',
								width: 180,
								align: 'center'
							}, {
								field: 'rcConfing',
								title: '产品描述',
								width: 180,
								align: 'center'
							}, {
								field: 'rcAmount',
								title: '产品数量',
								width: 70,
								align: 'center'
							}, {
								field: 'prdCostPrice',
								title: '产品单价（元）',
								width: 110,
								align: 'center'
							}, {
								field: 'rcAmountUnit',
								title: '产品单位',
								width: 110,
								align: 'center'
							},{
								field: 'city',
								title: '操作',
								width: 80,
								templet: '#formulate2',
								align: 'center'
							}
						]
					],
					done: function(res, curr, count) {
						//如果是异步请求数据方式，res即为你接口返回的信息。
						//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
						pd=res.data
					}
				});
				
				table.on('edit(table)', function(obj) {
					console.log(pd)
					for (var i = 0; i < pd.length; i++) {
						if(obj.data.rcAmount>pd[i].rcAmount){
							layer.alert("出库数量不能大于原有数量！")
							obj.data.rcAmount=pd[i].rcAmount
							return;
						}
					}
					
					
				
				});

				form.on("submit(doSubmit)", function(data) {
					let outRepertoryVo = getJson("#dataFrm");

					console.log(ckcpxx)
					// var params2=params+"&productId="+oldount.productId+"&productName="+oldount.productName+"&productDescribe="+oldount.productDescribe+"&amount="+oldount.amount+"&amountUnit="+oldount.amountUnit+"&costPrice="+oldount.costPrice
					// var params2=params+"&OutDetails="+ckcpxx

					let datass = {
						OutDetails: ckcpxx,
						outRepertoryVo
					}

					/* for (var i = 0; i < datass.OutDetails.length; i++) {
						delete datass.OutDetails[i].lay_TABLE_INDEX;
						delete datass.OutDetails[i].LAY_TABLE_INDEX;
					} */

					console.log("aaa", ckcpxx)
					// console.log(params2)
					let par = $("#dataFrm").serialize();
					let reason = $("#reason").val();
					let storer = $("#storer").val();
					let remark = $("#remark").val();

					/* 
					 reason: "C002-1"
					 remark: "222"
					 storer: "12321"*/
					$.ajax({
						url: api2+ "outrepertory/addOutRepertoryShenqing?storer=" + storer + "&reason=" + reason + "&remark=" +
							remark,
						type: 'POST',
						data: JSON.stringify(ckcpxx),
						dataType: 'json',
						contentType: 'application/json;charset=utf-8',
						success: function(data) {
							layer.msg(data.msg);
							    if (data.code == 200) {
							        $('#dataFrm')[0].reset();
									console.log(ckcpxx)
									ckcpxx:[]
							    }
						},
						error: function(data) {
							console.log(data.status);
						}
					});

				})

			});
		</script>
		<script type="text/html" id="forType">
			{{# if(d.type=='Y001-1'){d.type='商品'}}
				{{d.type}}
			{{# }else{d.type='物料'}}
				{{d.type}}
			{{# }}}
		</script>
		<script type="text/html" id="forProductClass">
			{{# if(d.productClass=='D001-1'){d.productClass='高档'}}
				{{d.productClass}}
			{{# }else if(d.productClass=='D001-2'){d.productClass='中档'}}
				{{d.productClass}}
			{{# }else{d.productClass='抵挡'}}
				{{d.productClass}}
			{{# }}}
		</script>
		<script type="text/html" id="formulate">
			<button type="button" class="layui-btn layui-btn-xs" lay-event="edit">删除</button> 
		</script>
		<script type="text/html" id="formulate2">
			<button type="button" class="layui-btn layui-btn-xs"  lay-event="edit">出库</button> 
		</script>

	</body>
	<script src="../../../lib/serializejson/jquery.serializejson.js"></script>
</html>
